from rest_framework.viewsets import ModelViewSet
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView, RetrieveUpdateDestroyAPIView, ListCreateAPIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import mixins
from .models import Course
from .serializers import CourseSerializers


class CourseApiView(APIView):
    def get(self, request):
        """获取所有信息"""
        queryset = Course.objects.all()
        serializer = CourseSerializers(instance=queryset, many=True)
        return Response(serializer.data)

    def post(self, request):
        """添加信息"""
        serializer = CourseSerializers(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(status=status.HTTP_201_CREATED)


class CourseInfoView(APIView):
    def get(self, request, pk):
        """获取单个信息"""
        try:
            queryset = Course.objects.get(pk=pk)
        except Course.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = CourseSerializers(queryset)
        return Response(serializer.data)

    def put(self, request, pk):
        """更新单个信息"""
        try:
            queryset = Course.objects.get(pk=pk)
        except Course.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = CourseSerializers(instance=queryset, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(status=status.HTTP_200_OK)

    def delete(self, request, pk):
        """删除一个信息"""
        Course.objects.get(pk=pk).delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


class CourseGenericApiView(GenericAPIView):
    queryset = Course.objects
    serializer_class = CourseSerializers

    def get(self, request):
        """获取所有数据"""
        course_list = self.get_queryset()
        serializer = self.get_serializer(instance=course_list, many=True)
        return Response(serializer.data)

    def post(self, request):
        """添加数据"""
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)


class CourseInfoGenericApiView(GenericAPIView):
    queryset = Course.objects
    serializer_class = CourseSerializers

    def get(self, request, pk):
        """获取单条数据"""
        course = self.get_object()
        serializer = CourseSerializers(instance=course)
        return Response(serializer.data)

    def put(self, request, pk):
        """更新数据"""
        course = self.get_object()
        serializer = CourseSerializers(instance=course, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_200_OK)

    def delete(self, request, pk):
        self.get_object().delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


class CourseInfoMixinGenericApiView(RetrieveUpdateDestroyAPIView):
    queryset = Course.objects
    serializer_class = CourseSerializers

    def get(self, request, pk):
        """获取一条数据"""
        return self.retrieve(request, pk=pk)

    # def post(self, request):
    #     """添加一条数据"""
    #     return self.create(request)

    def delete(self, request, pk):
        """删除一条数据"""
        return self.destroy(request, pk=pk)

    def put(self, request, pk):
        """更新一条数据"""
        return self.update(request, pk=pk)


class CourseMixinView(ListCreateAPIView):
    queryset = Course.objects
    serializer_class = CourseSerializers

    def get(self, request):
        """获取所有数据"""
        return self.list(request)

    def post(self, request):
        """添加一条数据"""
        return self.create(request)


class CourseModelViewSet(ModelViewSet):
    queryset = Course.objects
    serializer_class = CourseSerializers

